package com.aigou.admin.aigouspringboot.controller.admin;


import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.OrderProduct;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.OrderProductService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/orderproduct")
@Slf4j
public class OrderProductController {
    @Resource
    private OrderProductService orderProductService;
    @GetMapping
    @LogScope(title = "商品订单管理模块", content = "查看商品订单列表信息")
    public Result select(@RequestParam Integer current) {

        log.info("查询第{}页的订单信息", current);
        // 分页查询 都返回PageInfo(分页插件工具)
        PageInfo<OrderProduct> orderproductPageInfo = orderProductService.selectOrderProductListByPage(current);
        return Result.success(orderproductPageInfo);
    }

    @GetMapping("/{id}")
    @LogScope(title = "商品订单管理模块", content = "根据id查询商品订单信息")
    public Result detail(@PathVariable String id){

        OrderProduct orderproduct = orderProductService.selectById(id);
        return Result.success(orderproduct);
    }

    @DeleteMapping("/{id}")
    @LogScope(title = "商品订单管理模块", content = "根据id删除商品订单信息")
    public Result delete(@PathVariable String id){

        int i = orderProductService.deleteById(id);
        return i>0 ? Result.success() : Result.error();
    }



    @PutMapping
    @LogScope(title = "商品订单管理模块", content = "编辑商品订单信息")
    public Result edit(@RequestBody  OrderProduct orderproduct){

        int i = orderProductService.edit(orderproduct);
        return i>0 ? Result.success() : Result.error();
    }

    @PostMapping("/save")
    @LogScope(title = "商品订单管理模块", content = "添加商品信息")
    public Result save(@RequestBody OrderProduct orderproduct){
        orderproduct.setId(RandomUtil.getRandomNumber());
        int i = orderProductService.save(orderproduct);
        return i>0 ? Result.success() : Result.error();
    }
}
